<?php
namespace Shakemobile\Controller;
use Think\Controller;
class ExtendController extends Controller {
	public $user;
	public function  __construct (){
		parent::__construct();
		Vendor("taobaosdk.top.TopClient");
		Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
		Vendor("taobaosdk.top.RequestCheckUtil");
		$sid = I("sid");
		$sid = 116841492;
		if($sid){
			cookie("sid",$sid);
			$this->sid = $sid;
			//cookie保存sid，读取不到则跳出
		}else{
			$this->sid = cookie("sid");
		}
		if(!$this->sid){
			// 没有店铺信息不能访问
			$this->assign("msg","抱歉，本活动只能在店铺内访问");
			$this->display("Index:not_allow");
			die();
		}
		$this->mixednick = cookie("tida_mixnick");
		$this->mixednick = "k01UulQpdrfWP/FlNTAiJ3nWf8p4y2Nzgo7UGObn3+dFq0=";
		// print_r( $_COOKIE);die();
		if(!$this->mixednick){
			// 没有用户信息禁止访问
			
			$this->assign("login",true);
			$this->assign("msg","抱歉，没有读取到您的用户信息，可能您没有登录，请登陆后重试");
			$this->display("Index:not_allow");
			die();
		}
		// $this->user = S($this->mixednick);
		if(!$this->user){
			$USR = D("Friends");
			$data["sid"]=$this->sid;
			$data["mixed_nick"]=$this->mixednick;
			$join = current($USR->where($data)->limit(1)->getField("id,join_time,score_time"));
			if(!$join){
				$CFG = M("ShopCfg");
				$tms_newcomer = $CFG->where(Array("sid"=>$this->sid))->getField("tms_newcomer");
				
				$data["score"]=$tms_newcomer;
				$data["join_time"]=date("Y-m-d H:i:s");
				$data["score_time"]=date("Y-m-d H:i:s");
				$data["friend_link"]="";
				$USR->add($data);
			}else{
				if(date("Ymd",strtotime($join["score_time"]))!=date("Ymd")){
					$CFG = M("ShopCfg");
					$tms_everyday = $CFG->where(Array("sid"=>$this->sid))->getField("tms_everyday");
					$sa["score_time"]=date("Y-m-d H:i:s");
					$sa["score"]=array('exp',' score+ '. $tms_everyday);
					$USR->where(Array("id"=>$join["id"]))->save($sa);
				}
				$data["join_time"]=$join["join_time"];
				$data["score_time"]=$sa["score_time"]?$sa["score_time"]:$join["score_time"];
				$data["friend_link"]="";
			}
			$this->user = $data;
			S($this->mixednick,$this->user);
		}

		
		$this->assign("appkey",C("FT_KEY"));
		$this->assign("appname",cookie("appname"));
		$this->assign("codeimg",$codeimg);
		$this->assign("sid",$this->sid);
		$this->assign("user",$this->user);
	}
}